Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VPC: Extend support for SG's #1989

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cjschaef
Copy link
Contributor

@cjschaef cjschaef commented Oct 3, 2024

What this PR does / why we need it:

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

/area provider/ibmcloud

  1. Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

Release note:

Add reconciliation support for extended VPC Security Groups

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. area/provider/ibmcloud Issues or PRs related to ibmcloud provider labels Oct 3, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: cjschaef
Once this PR has been reviewed and has the lgtm label, please assign prajyot-parab for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Oct 3, 2024
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Oct 3, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @cjschaef. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Oct 3, 2024
Copy link

netlify bot commented Oct 3, 2024

Deploy Preview for kubernetes-sigs-cluster-api-ibmcloud ready!

Name Link
🔨 Latest commit a2baa03
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-sigs-cluster-api-ibmcloud/deploys/67082973c48c370008facd33
😎 Deploy Preview https://deploy-preview-1989--kubernetes-sigs-cluster-api-ibmcloud.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@cjschaef cjschaef changed the title WIP: VPC: Extend support for SG's VPC: Extend support for SG's Oct 3, 2024
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 3, 2024
@mkumatag
Copy link
Member

mkumatag commented Oct 7, 2024

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 7, 2024
@Karthik-K-N
Copy link
Contributor

@dharaneeshvrd please take a look when get some time. Thank you.

Copy link
Contributor

@Karthik-K-N Karthik-K-N left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass, SG workflow looks bit complicated

// If we don't have an ID at this point, we assume we need to create the Security Group.
vpcID, err := s.GetVPCID()
if err != nil {
return false, fmt.Errorf("error retrieving vpc id for security group creation")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its better to log the err.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, yes I added the original err to the return value.

return false, fmt.Errorf("error retrieving vpc id for security group creation")
}
resourceGroupID, err := s.GetResourceGroupID()
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, added the original error.

return false, fmt.Errorf("error failed to create security group: %w", err)
}
if securityGroupDetails == nil {
s.V(3).Info("error failed creating security group", "name", securityGroup.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general while logging, Instead of using generic term like "name", If we log key as "securitygroupname" or somthing, In the observability tools it will be easy to filter and debug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, sorry, missed that. updating the logs that contain generic var names.

if securityGroupDetails == nil {
s.V(3).Info("error failed creating security group", "name", securityGroup.Name)
return false, fmt.Errorf("error failed creating security group")
} else if securityGroupDetails.ID == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know its defensive check, But is there any case happening like this ID or CRN being nil with no errors from create call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same situation, seems like a safer method that resulting in go panics.
I'll remove the additional checks of pointer fields.

// Add a tag to the Security Group for the cluster.
err = s.TagResource(s.IBMVPCCluster.Name, *securityGroupDetails.CRN)
if err != nil {
return false, fmt.Errorf("error failed to tag security group %s: %w", *securityGroupDetails.CRN, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought, if tagging fails here, In next reconcile we find id in status and we return, so that resource never being tagged.
May be need to revisit how better we can tag a resource.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the same situation occurs for the other resources. I will add the same comment here, that likely a found resource may need to be checked if it should be tagged.....knowing that existing resources may not have been created by CAPI, and thus should not be tagged.

var securityGroupID *string
if securityGroup.Name != nil {
securityGroupID = s.getSecurityGroupIDFromStatus(*securityGroup.Name)
} else if securityGroup.ID != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ID is set why can't we directly use it, Why do you think we need to iterate over status? If ID is not found in Cloud, In reconcileSecurityGroup func we are returning error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure why this safety check is bad, as we attempt to confirm the Security Group exists (ID is valid).
At this point with the code path, this is very unlikely, but it is a safety mechanism.

I will remove, but feel like these kind of checks are safer for future changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, My point is anyhow we are checking for id in previous step.

}

// If the SecurityGroup has no rules, we have nothing more to do for this Security Group.
if len(securityGroup.Rules) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this check first thing in this function so we can return early

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update.

}

// If the Security Group has no Rules at all, we simply create all the Rules
if existingSecurityGroupRuleIntfs == nil || existingSecurityGroupRuleIntfs.Rules == nil || len(existingSecurityGroupRuleIntfs.Rules) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is existingSecurityGroupRuleIntfs.Rules == nil required? anyhow we are checking len()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update.

if err != nil {
return false, fmt.Errorf("error failed creating all security group rule remotes: %w", err)
}
s.V(3).Info("Created security group rules")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since its in V(3) level can we include some more info like, security group id or rule?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update.

Add support to reconcile SecurityGroups and
SecurityGroupRules for VPC extended Infrastructure
support.

Related: kubernetes-sigs#1896
@cjschaef
Copy link
Contributor Author

Updates made, basic testing appears to be okay.

Copy link
Contributor

@Karthik-K-N Karthik-K-N left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few smaller things, Can be ignored if no other comments from others. Thank you. LGTM.


// Option #1: If the SecurityGroupRuleRemoteSecurityGroupReference has a name assigned, we can shortcut and simply check that
if sgRule.Name != nil && *securityGroupRuleRemote.SecurityGroupName == *sgRule.Name {
s.V(3).Info("security group rule remote security group name matches", "securityGroupRuleRemoteSecurityGroupName", sgRule.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be?

Suggested change
s.V(3).Info("security group rule remote security group name matches", "securityGroupRuleRemoteSecurityGroupName", sgRule.Name)
s.V(3).Info("security group rule remote security group name matches", "securityGroupRuleRemoteSecurityGroupName", *sgRule.Name)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update

// Option #3. We can compare the Security Group CRN (need ot lookup the CRN for securityGroupRemote)

// Option #1: If the SecurityGroupRuleRemoteSecurityGroupReference has a name assigned, we can shortcut and simply check that
if sgRule.Name != nil && *securityGroupRuleRemote.SecurityGroupName == *sgRule.Name {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since your checking for sgRule.Name for nil, may be starting second condition from it would be good.

Suggested change
if sgRule.Name != nil && *securityGroupRuleRemote.SecurityGroupName == *sgRule.Name {
if sgRule.Name != nil && *sgRule.Name == *securityGroupRuleRemote.SecurityGroupName{

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update

return true, nil
}
if *ipRule.Address == infrav1beta2.CIDRBlockAny {
s.V(3).Info("security group rule remote address matches 0.0.0.0/0")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s.V(3).Info("security group rule remote address matches 0.0.0.0/0")
s.V(3).Info("security group rule remote address matches %s", infrav1beta2.CIDRBlockAny)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update

Copy link
Contributor

@dharaneeshvrd dharaneeshvrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall SG related changes look's good to me.

@@ -299,6 +300,36 @@ func (s *VPCClusterScope) GetResourceGroupID() (string, error) {
return *resourceGroup.ID, nil
}

// GetSecurityGroupID returns the ID of a security group, provided the name.
func (s *VPCClusterScope) GetSecurityGroupID(name string) (*string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this func is getting used. Do you need this func?
If you need for future use, you can replace the checking from status part with getSecurityGroupIDFromStatus() to avoid code duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will remove this function.

Comment on lines +1168 to +1173
// Check the Status if an ID is already available for the Security Group.
if s.NetworkStatus() != nil && s.NetworkStatus().SecurityGroups != nil {
if id, ok := s.NetworkStatus().SecurityGroups[*securityGroup.Name]; ok {
securityGroupID = &id.ID
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you call getSecurityGroupIDFromStatus() from here to avoid code duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update

}

// Reconcile each Security Groups's Rules.
requeue = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if requeue is true, it would have return from prev block, it won't come here. Why do you want to set requeue to false again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update

// Check the Status if an ID is already available for the Security Group.
if s.NetworkStatus() != nil && s.NetworkStatus().SecurityGroups != nil {
if id, ok := s.NetworkStatus().SecurityGroups[*securityGroup.Name]; ok {
securityGroupID = &id.ID
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you found securityGroupID, don't you need to go to line 1187?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will refactor this block

Comment on lines +1176 to +1183
if securityGroupDetails, err := s.VPCClient.GetSecurityGroupByName(*securityGroup.Name); err != nil {
// If the Security Group was not found, we expect it doesn't exist yet, otherwise result in an error.
if _, ok := err.(*vpc.SecurityGroupByNameNotFound); !ok {
return false, fmt.Errorf("error failed lookup of security group by name: %w", err)
}
} else if securityGroupDetails != nil && securityGroupDetails.ID != nil {
securityGroupID = securityGroupDetails.ID
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you find id via this, then again checking in cloud for its existence in next block is not making sense to me, since it's already returned by cloud API only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will refactor this block

s.V(3).Info("Created security group rules", "securityGroupID", securityGroupID, "securityGroupRule", securityGroupRule)

// Security Group Rules do not have a Status, so we likely don't need to requeue, but for now, will requeue to verify the Security Group Rules
return true, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly do you want to verify here?
Requeuing for every SG rule creation will overload the controller.
Not needed IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SG Rules are created in bulk, for each new SG.
Verifying the SG Rules for an existing SG require more complex logic to try to prevent creating duplicates (this also applies when verifying all SG Rules were created properly, similar to the other resource types).

I'll drop verification of created SG Rules then.

}

// Security Group Rules do not have a Status, so we likely don't need to requeue, but for now, will requeue to verify the Security Group Rules
return true, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code will never reach here, please handle the else case here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the code will be reached after a SG Rule is first created.
If we do not wish to follow up SG Rule creation with verification, then this would be unreachable code, and would be refactored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/provider/ibmcloud Issues or PRs related to ibmcloud provider cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants